<template>
    <div />
</template>
<script>
    import { writeData, selectFile } from '@/util/file-action'
    import { getVideoInfo } from '@/util/video-details'
    import { getImgBase64 } from '@/util/get-img-base64/index'
    export default {
        name: 'AddVideo',
        data() {
            return {
                videoList: [],
                worker: null,
                len: 0,
            }
        },
        mounted() {
            this.worker = this.$worker.create([
                {
                    message: 'getVideoInfo',
                    func: (e) => {
                        // console.log(e)
                    },
                },
            ])
        },
        watch: {
            videoList: {
                handler(nval) {
                    if (nval.length == this.len) {
                        this.worker = null
                        this.$store.dispatch('videos/setVideoList', nval)
                        writeData(nval, 'videos', 'video', true)
                        this.$store.commit('videos/setSpinning', false)
                    }
                },
                deep: true,
            },
        },
        methods: {
            async addVideo(type, paths = []) {
                this.$store.commit('videos/setSpinning', true)
                let { dirPath, files } = await selectFile(type, paths)
                console.log('selectFile: ', dirPath)
                // 避免重复添加
                for (let i = 0; i < files.length; i++) {
                    if (this.$store.state.videos.videoList.find((item) => item.name == files[i].name)) {
                        files.splice(i, 1)
                        i--
                    }
                }
                if (files.length > 0) {
                    this.len = this.videoList.length + files.length
                    this.firstTreat(files)
                } else {
                    this.$store.commit('videos/setSpinning', false)
                }
            },
            /**
             *
             * @param {Array} list
             * @param {string} type 覆盖还是添加['cover', 'add']
             */
            firstTreat(list, type = 'add') {
                let start = new Date()
                let end = 0
                for (const i in list) {
                    if (!list[i].cover) {
                        this.worker.postMessage('getVideoInfo', ['']).then(async () => {
                            const item = await getVideoInfo(list[i].name)
                            item.id = Date.now().toString(36)
                            item.name = list[i].name
                            item.path = list[i].path
                            console.log(item.cover)
                            let url = item.cover.replace('https://image11.m1905.cn/mdb/uploadfile/', '/mdb/')
                            url = url.replace('https://image11.m1905.cn/uploadfile/', '/coverImage1/')
                            item.imgSrc = await getImgBase64(url)
                            if (type == 'cover') {
                                this.$set(this.videoList, i, item)
                            } else if (type == 'add') {
                                this.videoList.push(item)
                            }
                            end = new Date()
                            console.log(end - start)
                        })
                    }
                }
            },
            // firstTreat(list, type = 'add') {
            //     let start = new Date()
            //     let raceList = []
            //     for (const i in list) {
            //         if (!list[i].cover) {
            //             raceList.push(getVideoInfo(list[i].name))
            //         }
            //     }
            //     let end = 0
            //     Promise.all(raceList).then(async (res) => {
            //         for (const i in res) {
            //             res[i].id = Date.now().toString(36)
            //             res[i].name = list[i].name
            //             res[i].path = list[i].path
            //             let url = res[i].cover.replace('https://image11.m1905.cn/mdb/uploadfile/', '/mdb/')
            //             url = url.replace('https://image11.m1905.cn/uploadfile/', '/coverImage1/')
            //             res[i].imgSrc = await getImgBase64(url)
            //             if (type == 'cover') {
            //                 this.$set(this.videoList, i, res[i])
            //             } else if (type == 'add') {
            //                 this.videoList.push(res[i])
            //             }
            //         }
            //         end = new Date()
            //         console.log(end - start)
            //     })
            // },
            setVideoList(val) {
                // console.log('setVideoList: ', JSON.stringify(val))
                if (val.length > 0) {
                    for (const i in val) {
                        this.$set(this.videoList, i, val[i])
                    }
                } else {
                    this.videoList = []
                }
            },
            returnVideoList() {
                return this.videoList
            },
        },
    }
</script>
